Page({
  data: {
    idcardFrontUrl: '/static/images/avatar_1.jpg',
    idcardBackUrl: '/static/images/avatar_2.jpg',
    gender: '0',
    name: '林俊杰',
    mobile: '13900000000',
    building: '',
    point: '',
    room: ''
  },
  onLoad({
    point,
    building,
    item,
    id
  }) {
    if (!id) {
      this.setData({
        point,
        building,
        room: item
      })
    } else {
      this.getRoomDetail(id)
      wx.setNavigationBarTitle({
        title: '编辑房屋信息',
      })
    }
    // 

  },
  // 获取房屋信息
  async getRoomDetail(id) {
    const {
      code,
      data,
      message
    } = await wx.http.get(`/room/${id}`)
    if (code !== 10000) return wx.utils.toast(message)
    const gender = {
      gender: data.gender.toString()
    }
    const roomDetail = Object.assign({}, data, gender)
    this.setData({
      ...roomDetail,
    })

  },
  // 上传图片
  async uploadPicture(e) {
    const type = e.mark.type
    const media = await wx.chooseMedia({
      count: 1,
      mediaType: ['image'], //文件类型
      sizeType: ['compressed'], //图片压缩
    })
    // console.log('media', media);
    wx.uploadFile({
      filePath: media.tempFiles[0].tempFilePath,
      name: 'file',
      url: wx.http.baseURL + '/upload',
      header: {
        Authorization: getApp().token,
      },
      success: (e) => {
        // console.log('成功', e);
        if (e.statusCode === 401) {
          const pageStack = getCurrentPages()
          const lastPage = pageStack[pageStack.length - 1]
          const redirectURL = lastPage.route
          wx.redirectTo({
            url: `/pages/login/index?redirectURL=/${redirectURL}`,
          })
          return
        }
        const data = JSON.parse(e.data)
        this.setData({
          [type]: data.data.url
        })
      }
    })
  },
  // 验证图片
  varifyImage() {
    // 两个地址不能为空
    const vaild = !!this.data.idcardFrontUrl && !!this.data.idcardBackUrl
    if (!vaild) {
      wx.utils.toast('请上传图片')

    }
    return vaild
  },
  // 验证名字
  varifyName() {
    const reg = /^[\u4e00-\u9fa5]{2,5}$/
    const vaild = reg.test(this.data.name.trim())
    if (!vaild) {
      wx.utils.toast('请填写真实的中文姓名')

    }
    return vaild
  },
  // 验证手机
  varifyMobile() {
    const reg = /^1[3-9][0-9]{9}$/
    const vaild = reg.test(this.data.mobile.trim())
    if (!vaild) {
      wx.utils.toast('请填写正确的手机号码')
    }
    return vaild
  },
  // 更改性别
  onChange(e) {
    // console.log('更改性别', e);
    this.setData({
      gender: e.detail
    })
    console.log(this.data.gender);
  },
  // 提交审核
  async goList() {
    if (!this.varifyName()) return
    if (!this.varifyMobile()) return
    if (!this.varifyImage()) return
    delete this.data.__webviewId__
    delete this.data.status
    console.log(this.data);

    const gender = {
      gender: +this.data.gender
    }
    const roomMation = Object.assign({}, this.data, gender)
    //发请求
    const res = await wx.http.post('/room',
      roomMation
    )

    wx.navigateBack({
      delta: this.data.id ? 2 : 4,
      success: () => {
        console.log('添加');
      }
    })
    // wx.reLaunch({
    //   url: '/house_pkg/pages/list/index',
    // })
  },

  removePicture(ev) {
    // 移除图片的类型（身份证正面或反面）
    const type = ev.mark?.type
    this.setData({
      [type]: ''
    })
  },
})